<script setup lang="ts">
import { ref, onBeforeUnmount } from 'vue';
import { loginApi, sendCodeApi } from '@/apis/login';
import { useMemberStore } from '@/stores'


const params = ref({
  phone: '',
  code: ''
})

const time = ref(0);
const codeShow = ref(true)
const timer = ref()
const sendCode = () => {
  if (!(/^1[3-9]\d{9}$/.test(params.value.phone))) {
    uni.showToast({
      title: '请输入正确的手机号',
      icon: 'none',
    })
  } else {
    sendCodeApi(params.value.phone)
    codeShow.value = false;
    time.value = 60;
    timer.value = setInterval(() => {
      time.value--;
      if (time.value === 0) {
        codeShow.value = true;
        clearInterval(timer.value)
        timer.value = null;
      }
    }, 1000)
  }
}


const useMember = useMemberStore()
const login = async () => {
  if (!(/^1[3-9]\d{9}$/.test(params.value.phone))) {
    uni.showToast({
      title: '请输入正确的手机号',
      icon: 'none',
    })
    return;
  }
  if (params.value.code.replace(/\s/g, '').length !== 4) {
    uni.showToast({
      title: '请输入验证码',
      icon: 'none',
    })
    return;
  }
  loginApi(params.value).then(res => {
    if(res.code === 0){
      useMember.setProfile(res?.data)
      uni.switchTab({
        url: '/pages/index/index'
      });
    }else{
      uni.showToast({
        title: res.msg,
        icon: 'none',
      })
    }
  })
}

const clearTimer = () => {
  if (timer.value) {
    clearInterval(timer.value);
    timer.value = null;
  }
}

onBeforeUnmount(() => {
  clearTimer()
})

</script>

<template>
  <view class="viewport">
    <view class="logo">

    </view>
    <view class="login">
      <view class="login_form">
        <view class="view_input">
          <input class="input" v-model="params.phone" type="text" placeholder="请输入手机号码" />
        </view>
        <view class="view_input">
          <input class="input" v-model="params.code" type="text" placeholder="请输入验证码" />
          <button class="button_code" @tap="sendCode" v-if="codeShow" style="background-color: rgb(22, 119, 255, 0.9);">获取验证码</button>
          <button v-else class="button_code" :disabled="true" style="background-color: rgb(22, 119, 255, 0.5);">{{ time }}</button>
        </view>
        <button class="button phone" @tap="login">登录</button>
      </view>
      <view class="tips">登录/注册即视为你同意《宠物坊用户协议》和《宠物坊隐私协议》</view>
    </view>
  </view>
</template>

<style lang='scss'>
page {
  height: 100%;
}

.viewport {
  display: flex;
  flex-direction: column;
  position: relative;
  height: 100%;
}

.logo {
  text-align: center;
  height: 30%;

  image {
    width: 220rpx;
    height: 220rpx;
  }

}

.login {
  height: 70%;
  padding: 20rpx;

  .login_form {
    width: 100%;

    .input {
      width: 100%;
      height: 80rpx;
      font-size: 28rpx;
      border-radius: 72rpx;
      border: 1px solid #ddd;
      padding-left: 30rpx;
      margin-bottom: 20rpx;
    }
  }

  .view_input {
    width: 100%;
    display: flex;
    justify-content: space-between;

    .button_code {
      width: 300rpx;
      height: 80rpx;
      line-height: 80rpx;
      font-size: 26rpx;
      color: #fff;
      margin-left: 3%;
      border-radius: 72rpx;
      text-align: center;
    }
  }

  .button {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 20rpx;
    width: 100%;
    height: 80rpx;
    font-size: 28rpx;
    border-radius: 72rpx;
    color: #fff;

    .icon {
      font-size: 40rpx;
      margin-right: 6rpx;
    }
  }

  .phone {
    background-color: #28bb9c;
  }

  .wechat {
    background-color: #06c05f;
  }
}

.tips {
  position: absolute;
  bottom: 80rpx;
  left: 20rpx;
  right: 20rpx;
  font-size: 22rpx;
  color: #999;
  text-align: center;
}
</style>